package net.zjitc.list.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author Administrator
 * @Date 2020/6/12 10:23
 * JDBC工具类，获取数据库连接等操作
 **/
public class JDBCUtils {
    private static DataSource ds;
    static {
        /* 1、加载配置文件进内存
        *  2、初始化ds*/
        try {
            Properties pro=new Properties();
            /* 类加载器，读取src目录下的文件，将文件转为流对象*/
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds=DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取DataSource
     * @return
     */
    public static DataSource getDataSource(){
        return ds;
    }
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
